//
//  IBLeagueViewController.m
//  IBLeague
//
//  Created by 경민 이 on 10. 12. 5..
//  Copyright 2010 huskycode. All rights reserved.
//

#import "IBLeagueViewController.h"
#import <MobileCoreServices/UTType.h>
#import <QuartzCore/QuartzCore.h>

#import <objc/runtime.h>

#import "huskyCell.h"

@implementation IBLeagueViewController
@synthesize m_baseballGameData;


static NSString *huskyCellIdentifier = @"huskyCellIdentifier";

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

static NSString *gb_dataKey = @"identifier";

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    
	
	NSArray *imageArr = [[NSArray alloc] initWithObjects: @"1-01",@"2-01",@"3-01",@"4-01",@"5-01",@"6-01",@"7-01",@"8-01",@"9-01",@"0-01",nil];
	
	m_viewArray = [[NSMutableArray alloc] init];
	NSLog(@"view Array Created");
	int initX = 0 , initY = 310;
	UIImageView *numberButton = nil;
	for( int i = 0 ; i < 5 ; i++ )
	{
		for (int j = 0; j < 2 ; j++ ) {
			numberButton = [[[UIImageView alloc] initWithFrame:CGRectMake(initX+(64*i), initY+(50*j), 64.0f, 50.0f)] autorelease];
			numberButton.image = [UIImage imageNamed:[imageArr objectAtIndex:i+j*5]];
			[self.view addSubview:numberButton];
			[m_viewArray insertObject:numberButton atIndex:0];
		}
	}
	
	m_ControlViewArray = [[NSMutableArray alloc] init];
	initX = 5 , initY = 254;
	UIImageView *controlButton = nil;
	for( int i = 0 ; i < 3 ; i++ ) 
	{
		controlButton = [[[UIImageView alloc] initWithFrame:CGRectMake(initX+(80*i), initY, 64.0f, 50.0f)] autorelease];
		controlButton.backgroundColor = [UIColor whiteColor];
		controlButton.layer.cornerRadius = 10;
		[self.view addSubview:controlButton];
		
		
		
		[m_ControlViewArray insertObject:controlButton atIndex:0];
		
		objc_setAssociatedObject([m_ControlViewArray objectAtIndex:0], gb_dataKey , self, OBJC_ASSOCIATION_RETAIN);
	}
	
	m_dragImage = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 64.0f, 50.0f)] autorelease];
	m_dragImage.alpha = 0.4;
	[m_dragImage setHidden:YES];
	[self.view addSubview:m_dragImage];
	
	// <-- only for test
	NSDictionary *row1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"husky",@"type",@"let's start.",@"msg",nil ]; 
	NSDictionary *row2 = [[NSDictionary alloc] initWithObjectsAndKeys:@"user",@"type",@"ok, i'm ready.",@"msg",nil ]; 
	
	
	NSArray *tmpArr = [[NSArray alloc] initWithObjects:row1,row2,nil];
	self.m_baseballGameData = tmpArr;
	
	[row1 release];
	[row2 release];
	[tmpArr release];
	
	// only for test -->
	
	[imageArr release];
	[super viewDidLoad];			   
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
	m_touchStartPoint = [[touches anyObject] locationInView:self.view];
	NSLog(@"%f %f",m_touchStartPoint.x,m_touchStartPoint.y);
	for( UIImageView *view in m_viewArray )
	{
		if (CGRectContainsPoint(view.frame, m_touchStartPoint)) {
			
			m_dragImage.frame = CGRectMake(m_touchStartPoint.x - m_dragImage.frame.size.width/2.0, m_touchStartPoint.y-m_dragImage.frame.size.height/2.0, 64.0f, 50.0f);
			m_dragImage.image = [UIImage imageWithCGImage:view.image.CGImage];
			m_dragImage.layer.cornerRadius = 10;
			[m_dragImage setHidden:NO];
			break;
		}
	}
	
}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
	if( !m_dragImage.hidden )
	{
		CGPoint touchCurrentPoint = [[touches anyObject] locationInView:self.view];
		m_dragImage.frame = CGRectMake(touchCurrentPoint.x - m_dragImage.frame.size.width/2.0, touchCurrentPoint.y-m_dragImage.frame.size.height/2.0, 64.0f, 50.0f);
		
		for (int i = 0; i < [m_ControlViewArray count]; i++ ) {
			UIImageView *test = [m_ControlViewArray objectAtIndex:i];
			
			CALayer* dropLayer = test.layer;
			
			if(CGRectContainsPoint(test.frame, touchCurrentPoint) )
			{
				NSLog(@"touched %f %f",test.frame.origin.x,test.frame.origin.y);
				
				CGMutablePathRef path = CGPathCreateMutable();
				CGPathAddRect(path, NULL, dropLayer.bounds);
				test.layer.shadowPath = path;
				CGPathRelease(path);
				
				dropLayer.shadowOffset = CGSizeZero;
				dropLayer.shadowColor = [[UIColor blackColor] CGColor];
				dropLayer.shadowRadius = 6.0;				
				dropLayer.shadowOpacity = 1.0;
			}
			else {
				dropLayer.shadowOpacity = 0.0;
			}

		}
	}
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{

	CGPoint touchEndPoint = [[touches anyObject] locationInView:self.view];
	
	for (int i = 0; i < [m_ControlViewArray count]; i++ ) 
	{
		UIImageView *test = [m_ControlViewArray objectAtIndex:i];
		CALayer* dropLayer = test.layer;
		dropLayer.shadowOpacity = 0.0;
		if( CGRectContainsPoint(test.frame, touchEndPoint) )
		{
			test.image = m_dragImage.image;
		}
	}
	[m_dragImage setHidden:YES];	
}



/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

- (IBAction)buttonPressed:(id)sender
{
	NSLog(@"go!!");
}

#pragma mark -
#pragma mark Table View Data Source Methods

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return [self.m_baseballGameData count];
}

- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath
{
	//huskyCellIdentifier
	huskyCell *cell = (huskyCell *)[tableView dequeueReusableCellWithIdentifier:huskyCellIdentifier];
	
	if (cell == nil) {
		NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"huskyCell" owner:self options:nil];
		cell = [nib objectAtIndex:0];
	}
	
	NSUInteger row = [indexPath row];
	NSDictionary *rowData = [self.m_baseballGameData objectAtIndex:row];
	
	NSString *type = [rowData objectForKey:@"type"];
	cell.m_LBmsg.text = [rowData objectForKey:@"msg"];
	if ( [type isEqualToString:@"husky"]) {
		cell.m_LBmsg.textAlignment = UITextAlignmentLeft;
		cell.m_IVhusky.image = [UIImage imageNamed:@"husky-01"];
		cell.m_IVuser.backgroundColor = [UIColor whiteColor];
	}
	else {
		cell.m_LBmsg.textAlignment = UITextAlignmentRight;
		cell.m_IVuser.image = [UIImage imageNamed:@"human-01"];
		cell.m_IVhusky.backgroundColor = [UIColor whiteColor];		
	}

	return cell;
}

- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*) indexpath
{
	return 52;
}



@end
